//
//  RBGoodsEvaluateListVC.m
//  zhongzhenglu
//
//  Created by 马军凯 on 2023/5/9.
//

#import "RBGoodsEvaluateListVC.h"
#import "RBGoodsEvaluateListResponse.h" //model
#import "RBGoodsEvaluateListCell.h" //cell
#import "RBGoodsEvaluateListHeaderView.h" //头

@interface RBGoodsEvaluateListVC ()
{
    int _page;
    int _allPage;
}
//
@property (nonatomic, strong) RBGoodsEvaluateListResponse *response;
//tableView
@property (nonatomic, strong) BaseTableView *tableView;
//
@property (nonatomic, strong) RBGoodsEvaluateListHeaderView *headView;
//数据
@property (nonatomic, retain) NSMutableArray *dataArray;
//
@property (nonatomic, copy) NSString *type;
//
@property (nonatomic, assign) BOOL isFirst;

@end

@implementation RBGoodsEvaluateListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.type = @"-1";
    self.isFirst = YES;
    [self setup];
    [self createRefresh];
    [self headRefresh];
}
//
- (void)setup {
    self.title = @"全部评价";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
    
    LCWeakSelf;
    self.tableView.CreateCell = ^UITableViewCell * _Nonnull(NSIndexPath * _Nonnull indexPath) {
        RBGoodsEvaluateListCell *cell = [RBGoodsEvaluateListCell baseCellWith:weakSelf.tableView];
        //
        RBGoodsEvaluateDataModel *model = weakSelf.dataArray[indexPath.row];
        if (model) {
            cell.model = model;
        }
        
        return cell;
    };
}


#pragma mark 刷新
- (void)createRefresh {
    MJWeakSelf;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf headRefresh];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf footRefresh];
    }];
}
//刷新
- (void)headRefresh {
    _page = 1;
    [self.dataArray removeAllObjects];
    [self getData];
}
//加载更多
- (void)footRefresh {
    if (_page > _allPage) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } else {
        [self getData];
    }
}
//结束刷新
- (void)endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}
//
- (void)getData {
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"goods_id"] = weakSelf.goods_id;
        dicM[@"page"] = @(self->_page);
        dicM[@"type"] = weakSelf.type;
    } path:@"api/goods_evaluate_list" post:YES log:YES responseClass:RBGoodsEvaluateListResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBGoodsEvaluateListResponse *data) {
        [weakSelf receiveDataWith:data];
        [weakSelf endRefresh];
    } failBlock:^(RBGoodsEvaluateListResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
        [weakSelf endRefresh];
    } reloadBlock:nil];
}
//处理数据
- (void)receiveDataWith:(RBGoodsEvaluateListResponse *)allModel {
    if (self.isFirst == YES) {
        _isFirst = NO;
        self.headView.model = allModel.data;
    }
    //分页
    int total = [allModel.data.evaluate.total intValue];
    int per_page = [allModel.data.evaluate.per_page intValue];
    if (0 == total % per_page) {
        _allPage = total / per_page;
    } else {
        _allPage = total / per_page + 1;
    }
    _page = _page + 1;
    //数据
    for (RBGoodsEvaluateDataModel *model in allModel.data.evaluate.data) {
        [self.dataArray addObject:model];
    }
    self.tableView.rows = @[@(self.dataArray.count)];
    [self.tableView reloadData];
    if (self.dataArray.count == 0) {
        [self.view showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}

#pragma mark 懒加载
//头
- (RBGoodsEvaluateListHeaderView *)headView {
    if (_headView == nil) {
        _headView = kRBGetViewFromNib(@"RBGoodsEvaluateListHeaderView");
        LCWeakSelf;
        _headView.block = ^(NSString * _Nonnull type) {
            weakSelf.type = type;
            [weakSelf headRefresh];
        };
        [self.view addSubview:_headView];
        [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
        }];
    }
    return _headView;
}
//tableView
- (BaseTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[BaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = self.view.backgroundColor;
        _tableView.sections = 1;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
        [_tableView registerNibWith:@[@"RBGoodsEvaluateListCell"]];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.headView.mas_bottom).offset(10);
            make.left.right.bottom.mas_equalTo(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
        }];
        
    }
    return _tableView;
}
//数据
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
